package com.example.robertluozizhao.framecollectdemo.test;

import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.util.Log;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.DataOutputStream;
import java.io.InputStreamReader;
import java.io.File;
import java.io.RandomAccessFile;
import android.os.SystemClock;

import com.example.robertluozizhao.framecollectdemo.R;

/**
 * An example full-screen activity that shows and hides the system UI (i.e.
 * status bar and navigation/system bar) with user interaction.
 */
public class FullscreenActivity extends AppCompatActivity implements View.OnClickListener{
    public String TAG = "FullscreenActivity";
    public String m_strUseTime = "";
    public FileWrite m_pFileWrite = null;
    public int m_nTotalAll = 0;
    public int m_nTotalLast = 0;

    Handler m_pHandler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            setTipText(msg.what, msg.obj.toString());
        }

    };

    Runnable m_pRunnableTime = new Runnable() {

        private String getFileString(String strFile) {
            //Log.i(TAG,"getFileStringEx " + strFile + " begin");

            String strText = "";
            try {
                Runtime runtime = Runtime.getRuntime();
                Process process = runtime.exec("su");
                DataOutputStream os = new DataOutputStream(process.getOutputStream());
                os.writeBytes("cat " + strFile + "\n");
                os.flush();

                InputStream is = process.getInputStream();
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                while (null != (strText = br.readLine())) {
                    break;
                }
                //Log.i(TAG,"getFileStringEx size = "+ strText.length() +" Text = " + strText);
            } catch (IOException ex) {
                ex.printStackTrace();
                Log.e(TAG, "IOException " + ex.toString());
            }

            //Log.i(TAG,"getFileStringEx " + strFile + " end");
            return strText;
        }

        private String getUsedTime() {
            String strUseTime = "other";
            String strText = getFileString("/sys/kernel/debug/mmc0/mmc0:0001/ext_csd");
            if(strText != null) {
                String strTextA = strText.substring(536, 538);
                String strTextB = strText.substring(538, 540);
                int nUseTime = Integer.parseInt(strTextA, 16);
                switch (nUseTime)
                {
                    case 0x00:
                        strUseTime = "00%-00%";
                        break;
                    case 0x01:
                        strUseTime = "00%-10%";
                        break;
                    case 0x02:
                        strUseTime = "10%-20%";
                        break;
                    case 0x03:
                        strUseTime = "20%-30%";
                        break;
                    case 0x04:
                        strUseTime = "30%-40%";
                        break;
                    case 0x05:
                        strUseTime = "40%-50%";
                        break;
                    case 0x06:
                        strUseTime = "50%-60%";
                        break;
                    case 0x07:
                        strUseTime = "60%-70%";
                        break;
                    case 0x08:
                        strUseTime = "70%-80%";
                        break;
                    case 0x09:
                        strUseTime = "80%-90%";
                        break;
                    case 0x0A:
                        strUseTime = "90%-100%";
                        break;
                    case 0x0B:
                        strUseTime = "Exceeded its maximum estimated device life time";
                        break;
                    default:
                        break;
                }
            }

            return strUseTime;
        }

        @Override
        public void run() {
            String strUseTime = getUsedTime();
            if(m_strUseTime != strUseTime){
                int nValue = m_nTotalAll-m_nTotalLast;
                setTipText(R.id.text_last, "上一阶段写入数据 : "+ nValue +" MB");
                m_nTotalLast = m_nTotalAll;
                m_strUseTime = strUseTime;
            }

            setTipText(R.id.text_time, "使用时间 : "+strUseTime);
            m_pHandler.postDelayed(this, 5000);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_text_use_able_time);

        ((Button)findViewById(R.id.btn_play_start)).setOnClickListener(this);
        ((Button)findViewById(R.id.btn_play_stop)).setOnClickListener(this);

        m_pHandler.postDelayed(m_pRunnableTime, 100);
    }

    public void onClick(View arg0) {

        int btn = arg0.getId();
        if(btn == R.id.btn_play_start){
            if(m_pFileWrite == null){
                m_pFileWrite = new FileWrite();
                m_pFileWrite.start();
            }
        }else if(btn == R.id.btn_play_stop){
            if(m_pFileWrite != null) {
                m_pFileWrite.m_bExit = true;

                try {
                    m_pFileWrite.join();
                } catch (Exception ex) {
                    ex.printStackTrace();
                    Log.e(TAG, "IOException " + ex.toString());
                }

                m_pFileWrite = null;
            }
        }else{

        }
    }

    public void setTipText(int id, String strText){
        TextView view = (TextView)findViewById(id);
        if(view == null){
            return;
        }
        view.setText(strText);
    }

    public class FileWrite extends Thread{

        public boolean m_bExit = false;
        private int nSize = 1024*1024;
        private byte[] buffer = new byte[nSize];
        long lLastTime = 0;

        @Override
        public void run() {
            Log.i(TAG, "FileWrite in");

            while(!m_bExit) {
                File pFile = null;
                RandomAccessFile pRandomAccessFile = null;
                try {
                    pFile = new File("/sdcard/test.xxx");
                    if(pFile.exists()){
                        pFile.delete();
                    }
                } catch (Exception ex) {
                    ex.printStackTrace();
                    Log.e(TAG, "IOException " + ex.toString());
                }

                try {
                    pFile = new File("/sdcard/test.xxx");
                    pRandomAccessFile = new RandomAccessFile(pFile, "rw");
                } catch (IOException ex) {
                    ex.printStackTrace();
                    Log.e(TAG, "IOException " + ex.toString());
                }

                int nCount = 0;
                while (!m_bExit) {
                    if (pRandomAccessFile == null) {
                        break;
                    }

                    try {
                        pRandomAccessFile.write(buffer, 0, nSize);
                        nCount++;
                        m_nTotalAll++;
                    } catch (IOException ex) {
                        ex.printStackTrace();
                        Log.e(TAG, "IOException " + ex.toString());
                    }

                    try {
                        Thread.sleep(1);
                    } catch (InterruptedException ex) {
                        ex.printStackTrace();
                        Log.e(TAG, "InterruptedException " + ex.toString());
                    }

                    long lCurrentTime = SystemClock.elapsedRealtime();
                    if(lCurrentTime - lLastTime >= 1000) {
                        Message msg = m_pHandler.obtainMessage(R.id.text_file,"写入数据 : " + m_nTotalAll + " MB");
                        m_pHandler.sendMessage(msg);
                        lLastTime = lCurrentTime;
                    }

                    if (nCount == 1024) {
                        break;
                    }
                }

                try {
                    Log.i(TAG,"File size = " + pRandomAccessFile.length());
                    pRandomAccessFile.close();
                    pRandomAccessFile = null;
                } catch (IOException ex) {
                    ex.printStackTrace();
                    Log.e(TAG, "IOException " + ex.toString());
                }
            }

            Log.i(TAG, "FileWrite out");
        }
    }
}
